package com.example.tvmatchddemo;


import static com.example.tvmatchddemo.data.Contans.HAND_FOUCS_DOWN;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.tvmatchddemo.data.Contans;
import com.example.tvmatchddemo.util.DebugLog;


/**
 * created by liangkun on ${DATA}
 * Describe:
 */

public class TvCustomLayout extends RelativeLayout {

    private TextView tvFont;
    private ImageView ivDown;
    private RelativeLayout rlContainer;
    private Context context;

    public TvCustomLayout(Context context) {
        super(context);


    }

    public TvCustomLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TvCustomLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
//        覆盖子类控件而直接获得焦点
        View view = LayoutInflater.from(context).inflate(R.layout.tv_custom_view, this);
        initView(view);
        initAttrs(context, attrs, defStyleAttr);
        initEvent();
    }


    /**
     * 自定义veiw
     *
     * @param view
     */
    private void initView(View view) {
        tvFont = view.findViewById(R.id.tv_tab);
        ivDown = view.findViewById(R.id.iv_down);
        rlContainer = view.findViewById(R.id.rl_container);
    }

    /**
     * 事件
     */
    private void initEvent() {
        rlContainer.setOnFocusChangeListener(new OnFocusChangeListener() {
            @Override

            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    tvFont.setTextColor(context.getResources().getColor(R.color.white));
                    ivDown.setVisibility(VISIBLE);
                    if (listener != null) {
                        listener.seleteFoucsTab((Integer) v.getTag());

                    }

                } else {
                    //如果失去焦点
                    ivDown.setVisibility(INVISIBLE);
                    //如果tab焦点下移
                    if (Contans.HAND_MEMORY_FOUCS == HAND_FOUCS_DOWN) {
                        //保持高亮
                        tvFont.setActivated(true);
                        if (listener != null) {
                            listener.setActivatedTab((Integer) v.getTag());
                        }
                        return;
                    }
                    //如果焦点不下移
                    tvFont.setTextColor(context.getResources().getColor(R.color.home_select_text_blue));
                    if (listener != null) {
                        listener.unSeleteFoucsTab((Integer) v.getTag());
                    }
//                    tvFont.setActivated(true);
                }

            }
        });
    }

    /**
     * 初始化参数
     *
     * @param context
     * @param attrs        属性
     * @param defStyleAttr 属性
     */
    private void initAttrs(Context context, AttributeSet attrs, int defStyleAttr) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TvCustomLayout, defStyleAttr, 0);
//        TypedArray typedArray = context.obtainStyledAttributes(R.styleable.TvCustomLayout);
        int indexCount = typedArray.getIndexCount();
        for (int i = 0; i < indexCount; i++) {
            initAttr(typedArray.getIndex(i), typedArray);
        }
        typedArray.recycle();

    }

    private void initAttr(int attr, TypedArray typedArray) {
        if (attr == R.styleable.TvCustomLayout_item_product_icon) {
            //设置图标
            setDownIcon(typedArray.getDrawable(attr));
        }
        if (attr == R.styleable.TvCustomLayout_item_product_title) {
            setTitle(typedArray.getText(attr));
        }
        if (attr == R.styleable.TvCustomLayout_item_product_color) {
            setTitleColor(typedArray.getColor(attr, context.getResources().getColor(R.color.home_select_text_blue)));
        }
        if (attr == R.styleable.TvCustomLayout_tab_tag) {
//            setTitleColor(typedArray.getColor(attr, context.getResources().getColor(R.color.home_select_text_blue)));
            setTitleTag(typedArray.getInt(attr, -1));
        }
    }

    private void setTitleTag(int tag) {
        rlContainer.setTag(tag);
    }

    private void setTitleColor(int color) {

        tvFont.setTextColor(color);
    }

    private void setTitle(CharSequence text) {
        DebugLog.d("Hsc " + text.toString());
        tvFont.setText(text);
    }

    private void setDownIcon(Drawable drawable) {
        ivDown.setImageDrawable(drawable);
    }


    public interface FoucsFragmentListener {
        /**
         * 选择焦点title的
         *
         * @param tag 控件
         */
        void seleteFoucsTab(int tag);

        /**
         * 失去
         * @param tag
         */

        void unSeleteFoucsTab(int tag);

        /**
         * 向下按键保存常量的方法
         *
         * @param tag
         */
        void setActivatedTab(Integer tag);
    }

    private FoucsFragmentListener listener;

    public void setFoucsFragmentListener(FoucsFragmentListener foucsFragmentListener) {
        this.listener = foucsFragmentListener;
    }
}
